{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec83ccb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append(os.path.abspath(os.path.pardir))\n",
    "sys.path.append(os.path.abspath(os.path.pardir)+'/voltaml')\n",
    "\n",
    "from argparse import ArgumentParser\n",
    "\n",
    "import torch\n",
    "from voltaml.compile import VoltaGPUCompiler\n",
    "from voltaml.inference import gpu_performance\n",
    "import torchvision\n",
    "from voltaml.models.common import DetectMultiBackend\n",
    "from voltaml.models.yolov6.utils.checkpoint import load_checkpoint\n",
    "from voltaml.models.yolov6.layers.common import DetectBackend\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "# os.chdir('../voltaml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "256f752f-d0c1-459c-b8c8-302e3caf72d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download a pretrained model\n",
    "# import torch\n",
    "torch.hub.download_url_to_file('https://github.com/meituan/YOLOv6/releases/download/0.2.0/yolov6s.pt', 'yolov6s.pt')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff3892df",
   "metadata": {},
   "source": [
    "### Load models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eacd4457-ed7d-4cb2-8454-62386907805a",
   "metadata": {},
   "outputs": [],
   "source": [
    "torch_model_dir = 'yolov6s.pt'\n",
    "device = torch.device(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "588b6de6-3fc2-4c3f-b90d-a452a9659ac2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Load PyTorch model\n",
    "model = load_checkpoint(torch_model_dir, map_location=device, inplace=True, fuse=True)  # load FP32 model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91758d3d",
   "metadata": {},
   "source": [
    "## Set parameters for FP16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad6b0369",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_shape = (4,3,640,640)\n",
    "precision = 'fp16'\n",
    "compiled_model_dir = 'yolov6m_16_640.engine' ## Compiled model directory\n",
    "is_yolo = True\n",
    "input_name = 'images'\n",
    "output_name = 'output'\n",
    "simplify = True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58f4f365",
   "metadata": {},
   "source": [
    "### Compile Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d078430b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "compiler = VoltaGPUCompiler(\n",
    "    model=model,\n",
    "    output_dir=compiled_model_dir,\n",
    "    input_shape=input_shape,\n",
    "    precision=precision,\n",
    "    input_name=input_name,\n",
    "    output_name=output_name,\n",
    "    simplify=simplify\n",
    "    \n",
    ")\n",
    "\n",
    "compiled_model = compiler.compile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4fdbf14",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "gpu_performance(compiled_model_dir, model, input_shape=input_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea3116e0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd8a5daa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1422ab34",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
